/*
 * @Author       : super-J
 * @Date         : 2022-03-31 20:57:46
 * @LastEditTime : 2022-03-31 20:57:47
 * @LastEditors  : super-J
 * @Description  :
 */
//解析URL
export function parseUrl(str:any):any{//JS正则表达式解析URL(协议/域名/端口/路径/参数/锚点)
   const options = {
      key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
      q: {
         name: "queryKey",
         parser: /(?:^|&)([^&=]*)=?([^&]*)/g
      },
      parser: {
         loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
      }
   }

   if (!str) {
      return '';
   }

   let m = options.parser.loose.exec(str) as any
   let uri = {}
   let i = 14

   while (i--) uri[options.key[i]] = m[i] || "";

   uri[options.q.name] = {};
   uri[options.key[12]].replace(options.q.parser, function (_:string, $1:string, $2:string) {
      if ($1) uri[options.q.name][$1] = $2;
   });

   return uri;
}
